/** 
 ** auth yohao
 ** datetime 2018-10-06 13:29
 **  version 1.0000
 **/
var yohao = (function () {
    var alertPage = function (type, duration, title, content) {
        //计算距离顶部高度
        var top = 16;
        //获取当前alert 高度
        function getTopInstance() {
            top = 16
            $(".yh-notification.right").each(function () {
                top += $(this).outerHeight() + 10;
            });
            return top;
        }
        /**
         * 
         * @param {* }type 类型 默认没有图标 error success info warn
         * @param {* }  duration 显示时间, 毫秒。 设为 0 则不会自动关闭
         * @param {*} title 标题
         * @param {*} content 内容
         */
        function AddNotification(type, duration, title, content) {
            //设置默认值
            if (duration == null || duration == undefined)
                duration = 2500;
            else if (duration <= 0)
                duration = 0;
            if (title == null || title == undefined || title == '')
                title = '提示';
            let typeHtml = '';

            switch (type) {
                case 'error':
                    typeHtml = '<i class = "yh-notification__icon iconfont icon-yohao-error" > </i>'
                    break;
                case 'success':
                    typeHtml = '<i class = "yh-notification__icon iconfont icon-yohao-success" > </i>'
                    break;
                case 'info':
                    typeHtml = '<i class = "yh-notification__icon iconfont icon-yohao-info2" > </i>'
                    break;
                case 'warn':
                    typeHtml = '<i class = "yh-notification__icon iconfont icon-yohao-warn" > </i>'
                    break;
                default:
                    break;
            }
            let top = getTopInstance();
            let html = $(`<div role="alert" class="yh-notification right" style="top: ${top}px; z-index: 2006; ">` +
                `${typeHtml}` +
                `<div class="yh-notification__group">` +
                `<h2 class="yh-notification__title">${title}</h2>` +
                '<div class="yh-notification__content">' +
                `<p>${content}</p>` +
                '</div>' +
                '<div class="yh-icon yh-notification__closeBtn iconfont icon-yohao-close"></div>' +
                '</div>' +
                '</div >');
            $('body').append(html)
            //添加进入动画
            html.animate({
                right: '16px'
            }).fadeIn(500);

            //绑定关闭事件
            $(".yh-notification__closeBtn").on("click", function () {
                let parent = $(this).parent().parent();
                parent.fadeTo(200, 0.001, function () { //fade
                    $(this).slideUp(200, function () { //slide up
                        $(this).remove(); //then remove from the DOM
                        InitNotification();
                    });
                });

            });
            if (duration != 0) {
                setTimeout(() => {
                    html.fadeTo(200, 0.001, function () { //fade
                        $(this).slideUp(200, function () { //slide up
                            $(this).remove(); //then remove from the DOM
                            InitNotification();
                        });
                    });
                }, duration);
            }

        }

        //删除时重新计算Alert 的Top
        function InitNotification() {
            top = 16;
            $(".yh-notification.right").each(function () {
                $(this).css('top', top);
                top += $(this).outerHeight() + 10;
            });
        }

        return AddNotification(type, duration, title, content);

    }
    var messagePage = function (type, duration, content, showclose) {
        var z_index = 2018;
        /**
         * 
         * @param {* }type 类型 默认没有图标 error success info warn
         * @param {* }  duration 显示时间, 毫秒。 设为 0 则不会自动关闭
         * @param {*} content 内容
         * @param {*} showclose   是否显示关闭按钮
         */
        function AddMessage(type, duration, content, showclose) {
            //设置默认值
            if (duration == null || duration == undefined)
                duration = 2500;
            else if (duration <= 0)
                duration = 0;
            let typeHtml = '';
            let closeHtml = '';
            if (showclose)
                closeHtml = `<i class="yh-message__closeBtn iconfont icon-yohao-close"></i>`;

            switch (type) {
                case 'error':
                    typeHtml = `<div role="alert" class="yh-message yh-message--error" style="z-index: ${z_index+1};">` +
                        `<i class="yh-message__icon iconfont icon-yohao-error"></i>`
                    break;
                case 'success':
                    typeHtml = `<div role="alert" class="yh-message yh-message--success" style="z-index: ${z_index + 1};">` +
                        `<i class="yh-message__icon iconfont icon-yohao-success"></i>`
                    break;
                case 'info':
                    typeHtml = `<div role="alert" class="yh-message yh-message--info" style="z-index: ${z_index + 1};">` +
                        `<i class="yh-message__icon iconfont icon-yohao-info2"></i>`
                    break;
                case 'warn':
                    typeHtml = `<div role="alert" class="yh-message yh-message--warn" style="z-index: ${z_index + 1};">` +
                        `<i class="yh-message__icon iconfont icon-yohao-warn"></i>`
                    break;
                default:
                    typeHtml = `<div role="alert" class="yh-message yh-message--info" style="z-index: ${z_index + 1};">` +
                        `<i class="yh-message__icon iconfont icon-yohao-info2"></i>`
                    break;
            }
            let html = $(`${typeHtml}` +
                `<p class="yh-message__content">${content}</p>` +
                `${closeHtml}` +
                '</div>')
            $('body').append(html)
            //添加进入动画
            html.animate({
                top: '20px'
            }).fadeIn(250);
            z_index += 1;
            //绑定关闭事件
            $(".yh-message__closeBtn").on("click", function () {
                let parent = $(this).parent();
                parent.fadeTo(200, 0.001, function () { //fade
                    $(this).slideUp(200, function () { //slide up
                        $(this).remove(); //then remove from the DOM
                    });
                });

            });
            if (duration != 0) {
                setTimeout(() => {
                    html.fadeTo(200, 0.001, function () { //fade
                        $(this).slideUp(200, function () { //slide up
                            $(this).remove(); //then remove from the DOM
                        });
                    });
                }, duration);
            }

        }

        return AddMessage(type, duration, content, showclose);
    }
    return {
        Notification: alertPage,
        Message: messagePage
    }
})();

// export default {
//     Notification: yohao.Notification,
//     Message: yohao.Message
// }